package cn.iocoder.yudao.im.controller.admin;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.im.domain.NoticeImGroup;
import cn.iocoder.yudao.im.domain.NoticeImGroupMessage;
import cn.iocoder.yudao.im.service.NoticeImGroupMessageService;
import cn.iocoder.yudao.im.service.NoticeImGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * <p>
 * 群消息 前端控制器
 * </p>
 *
 * @author ZCC
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/message/group")
public class NoticeImGroupMessageController {


    @Resource
    private NoticeImGroupMessageService groupMessageService;

    @Resource
    private NoticeImGroupService groupService;

    /**
     * 发送群聊消息
     *
     * @param vo 发送消息 消息体
     * @return CommonResult
     */
    @PostMapping("/send")
    public CommonResult sendMessage(@Valid @RequestBody NoticeImGroupMessage vo) {
        NoticeImGroup group = groupService.getById(vo.getGroupId());
        if (Objects.isNull(group)) {
            return CommonResult.error(500, "群聊不存在");
        }
        if (Boolean.TRUE.equals(group.getDeleted())) {
            return CommonResult.error(500, "群聊已解散");
        }
        return CommonResult.success(groupMessageService.sendMessage(vo));
    }

    /**
     * 撤回群聊消息
     *
     * @param id 消息id
     * @return CommonResult
     */
    @GetMapping("/recall")
    public CommonResult recallMessage(@NotNull(message = "消息id不能为空") @RequestParam("id") Long id) {
        groupMessageService.recallMessage(id);
        return CommonResult.success("");
    }

    /**
     * 拉取离线消息,消息将通过webSocket异步推送
     *
     * @param minId 消息id
     * @return CommonResult
     */
    @GetMapping("/pullOfflineMessage")
    public CommonResult pullOfflineMessage(@RequestParam Long minId) {
        groupMessageService.pullOfflineMessage(minId);
        return CommonResult.success("");
    }

    /**
     * 将群聊中的消息状态置为已读
     *
     * @param groupId 群组id
     * @return CommonResult
     */
    @PostMapping("/readMessage")
    public CommonResult readMessage(@RequestParam("groupId") Long groupId) {
        groupMessageService.readMessage(groupId);
        return CommonResult.success("");
    }

    /**
     * 获取消息已读用户列表
     *
     * @param groupId   群组id
     * @param messageId 消息id
     * @return CommonResult
     */
    @GetMapping("/findReadUsers")
    public CommonResult findReadUsers(@RequestParam Long groupId, @RequestParam Long messageId) {
        return CommonResult.success(groupMessageService.findReadUsers(groupId, messageId));
    }

    /**
     * 查询聊天记录
     *
     * @param groupId 群组id
     * @param page    分页
     * @param size    分页
     * @return CommonResult
     */
    @GetMapping("/history")
    public CommonResult recallMessage(@NotNull(message = "群聊id不能为空") @RequestParam Long groupId,
                                      @NotNull(message = "页码不能为空") @RequestParam Long page,
                                      @NotNull(message = "size不能为空") @RequestParam Long size) {
        return CommonResult.success(groupMessageService.findHistoryMessage(groupId, page, size));
    }
}
